package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.antimoney.util.SuspiciousReport;
import com.turing.post.antimoney.util.SuspiciousReportDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Mar 27, 2013
 */
@Stateless(mappedName = "antimoney.impl.SuspiciousReportDaoImpl")
@Remote
@Local
public class SuspiciousReportDaoImpl implements SuspiciousReportDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insert(SuspiciousReport sr) throws PCPSApplicationException {
		GeneralDao.insert(_em, sr);
	}

	@Override
	public void updateSuspiciousReport(SuspiciousReport sr)
			throws PCPSApplicationException {
		GeneralDao.update(_em, sr);
	}

	@Override
	public SuspiciousReport getSuspiciousReportById(BigInteger id) {
		return _em.find(SuspiciousReport.class, id);
	}

	/**
	 * 根据主键和上报状态查询
	 * 
	 * @param id
	 * @return
	 * @throws PCPSApplicationException
	 */
	@Override
	public SuspiciousReport getSuspiciousReportByIdandStatus(
			PCPSWebInput webInput, BigInteger id, String reportStatus) {
		if (webInput == null || id == null || PCPSUtil.isEmpty(reportStatus)) {
			return null;
		}
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		if("".equals(issuecompanyno)){
			return null;
		}
		String sql = "select * from SuspiciousReport where IssueCompanyNo=?1  and reportstatus=?2 and ReportId =?3  for update";
		Query query = _em.createNativeQuery(sql, SuspiciousReport.class);
		query.setParameter(1, issuecompanyno);
		query.setParameter(2, reportStatus);
		query.setParameter(3, id);
		return (SuspiciousReport) query.getSingleResult();
	}

	@Override
	public String getSuspiciousReportBatchNoByReportDate(String date,
			String icNo) {
		String sql = "select nvl(max(s.batchno),0) from SuspiciousReport s where s.issuecompanyno='"
				+ icNo
				+ "' and s.reportdate=to_date('"
				+ date
				+ "','yyyy-mm-dd')";
		Query query = _em.createNativeQuery(sql);
		String batchNo = query.getSingleResult().toString();
		return String.format("%04d", (Integer.parseInt(batchNo) + 1));
	}

	/**
	 * 根据条件查询可疑交易报告表清单
	 * 
	 * @param startdate
	 *            起始时间
	 * @param senddate
	 *            终止时间
	 * @param reportstatus
	 *            报告状态
	 * @param orxn
	 *            报告名称
	 */
	@SuppressWarnings("unchecked")
	public List<SuspiciousReport> getSuspiciousReportByCondition(
			PCPSWebInput webInput, String startdate, String enddate,
			String reportstatus, String orxn, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(startdate)
				|| PCPSUtil.isEmpty(enddate)) {
			return null;
		}
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		if("".equals(issuecompanyno)){
			return null;
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from SuspiciousReport where IssueCompanyNo=?3 and  reportDate>=to_date(?1,'yyyy-mm-dd hh24:mi:ss') and reportDate<=to_date(?2,'yyyy-mm-dd hh24:mi:ss') and orxn like '%"
				+ orxn + "%'";

		if (!PCPSUtil.isEmpty(reportstatus))
			sql = sql + " and reportstatus='" + reportstatus + "' ";

		sql = sql + " order by reportDate,batchNo desc";
		Query query = _em.createNativeQuery(sql, SuspiciousReport.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, startdate);
		query.setParameter(2, enddate);
		query.setParameter(3, issuecompanyno);
		// query.setParameter(4, "%" + orxn + "%");
		return query.getResultList();
	}

	/**
	 * 根据条件查询可疑交易报告表清单总数
	 * 
	 * @param startdate
	 *            起始时间
	 * @param senddate
	 *            终止时间
	 * @param reportstatus
	 *            报告状态
	 * @param orxn
	 *            报告名称
	 */
	public BigInteger getSuspiciousReportByConditionTotal(
			PCPSWebInput webInput, String startdate, String enddate,
			String reportstatus, String orxn) {
		if (webInput == null || PCPSUtil.isEmpty(startdate)
				|| PCPSUtil.isEmpty(enddate)) {
			return null;
		}
		String issuecompanyno = PCPSUtil.getIssueCompanyNo(webInput);
		if("".equals(issuecompanyno)){
			return BigInteger.ZERO;
		}
		String sql = "select nvl(count(*),0) from SuspiciousReport where IssueCompanyNo=?3 and reportDate>=to_date(?1,'yyyy-mm-dd hh24:mi:ss') and reportDate<=to_date(?2,'yyyy-mm-dd hh24:mi:ss')"
				+ " and orxn like '%" + orxn + "%'";
		if (!PCPSUtil.isEmpty(reportstatus))
			sql = sql + " and reportstatus='" + reportstatus + "' ";

		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startdate);
		query.setParameter(2, enddate);
		query.setParameter(3, issuecompanyno);
		return new BigInteger(query.getSingleResult().toString());
	}
}
